package com.framework.service.agent.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.framework.dao.agent.AgentIntegralDao;
import com.framework.entity.flow.FlowAgentIntegral;
import com.framework.entity.sys.AgentIntegral;
import com.framework.entity.sys.SysAgent;
import com.framework.entity.ter.IntegralAgentSn;
import com.framework.entity.ter.TerGoods;
import com.framework.entity.ter.TerMerchantReport;
import com.framework.exception.RRException;
import com.framework.service.*;
import com.framework.service.agent.AgentIntegralService;
import com.framework.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author wq
 * @date 2019/9/26 15:37
 * @description
 */
@Service
public class AgentIntegralServiceImpl extends ServiceImpl<AgentIntegralDao, AgentIntegral> implements AgentIntegralService {

    @Autowired
    private AgentService agentService;

    @Autowired
    private FlowAgentIntegralService flowAgentIntegralService;

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private MerchantService merchantService;

    @Autowired
    private IntegralAgentSnService integralAgentSnService;


    @Override
    public AgentIntegral getIntegral(String token) {

        SysAgent agent = agentService.queryAgentByToken(token);
        AgentIntegral integral = queryByAgentId(agent.getAgentId());

        if (integral == null) {
            integral = new AgentIntegral();
            integral.setAgentId(agent.getAgentId());
            integral.setCurrIntegral(0);
        }
        return integral;
    }

    @Override
    public AgentIntegral queryByAgentId(Long agentId) {
        AgentIntegral integral = selectOne(new EntityWrapper<AgentIntegral>().eq("agent_id", agentId));
        return integral == null ? null : integral;
    }

    @Override
    public List<FlowAgentIntegral> getFlowIntegral(String token) {
        SysAgent agent = agentService.queryAgentByToken(token);
        return flowAgentIntegralService.queryIncomeList(agent.getAgentId());
    }

    @Override
    public void redIntegralByPay(Long agentId, Integer totalIntegral, Long goodsId, Integer goodsNum) {
        AgentIntegral integral = queryByAgentId(agentId);
        //添加记录
        TerGoods terGoods = goodsService.queryGoodsById(goodsId);

        if(totalIntegral.intValue()!=terGoods.getIntegralPrice().intValue()*goodsNum.intValue()){
            throw new RRException("9000","支付积分数量不正确");
        }

        if (integral == null || integral.getCurrIntegral().intValue() < totalIntegral.intValue()) {
            throw new RRException("9000", "积分不足");
        }
        //扣除积分
        reduceIntegral(integral, totalIntegral);
        FlowAgentIntegral flowAgentIntegral = new FlowAgentIntegral();
        flowAgentIntegral.setAgentId(agentId);
        flowAgentIntegral.setCreateTime(new Date());
        flowAgentIntegral.setIncomeType(1);
        flowAgentIntegral.setType(0);
        flowAgentIntegral.setIntegral(totalIntegral);
        flowAgentIntegral.setRemark("购买了" + goodsNum + "台" + terGoods.getName());
        flowAgentIntegralService.insert(flowAgentIntegral);


    }

    @Override
    public void addActivIntegral(String sn, Integer integral) {
        TerMerchantReport report = merchantService.queryMerchantBySn(sn);

        if (report != null) {

            if (report.getDeptId().equals(1L)) {

                if(report.getTerminalType().intValue()!= 9 ){
                    return;
                }
                //只有通过后台订单划拨的才会返回积分
                IntegralAgentSn integralAgentSn = integralAgentSnService.findBySn(sn);

                if(integralAgentSn==null){
                    return;
                }
                //添加积分
                AgentIntegral agentIntegral = queryByAgentId(integralAgentSn.getAgentId());

                if (agentIntegral == null) {
                        agentIntegral = new AgentIntegral();
                        agentIntegral.setTotalIntegral(integral);
                        agentIntegral.setCurrIntegral(integral);
                        agentIntegral.setCreateTime(new Date());
                        agentIntegral.setAgentId(integralAgentSn.getAgentId());
                        insert(agentIntegral);
                    } else {
                        addIntegral(agentIntegral, integral);
                    }
                    //添加记录
                    FlowAgentIntegral flowAgentIntegral = new FlowAgentIntegral();
                    flowAgentIntegral.setAgentId(integralAgentSn.getAgentId());
                    flowAgentIntegral.setCreateTime(new Date());
                    flowAgentIntegral.setIncomeType(2);
                    flowAgentIntegral.setType(1);
                    flowAgentIntegral.setIntegral(integral);
                    flowAgentIntegral.setRemark("激活" + sn + ",得到积分" + integral);
                    flowAgentIntegralService.insert(flowAgentIntegral);

                    integralAgentSnService.dropSn(integralAgentSn,sn);
                }

        }

    }

    /**
     * 扣除积分
     *
     * @param integral
     * @param totalIntegral
     */
    public void reduceIntegral(AgentIntegral integral, Integer totalIntegral) {
        integral.setCurrIntegral((integral.getCurrIntegral() - totalIntegral));
        integral.setUpdateTime(new Date());
        updateById(integral);
    }

    /**
     * 添加积分
     *
     * @param integral
     * @param totalIntegral
     */
    public void addIntegral(AgentIntegral integral, Integer totalIntegral) {
        integral.setCurrIntegral((integral.getCurrIntegral() + totalIntegral));
        integral.setTotalIntegral((integral.getTotalIntegral() + totalIntegral));
        integral.setUpdateTime(new Date());
        updateById(integral);
    }

}
